package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:ahang
 * @create:2022-12-12 13:39
 * @Description:
 */
@RestController
public class CustomerController {

    @Autowired
    CustomerService customerService;

    /**
     * 分页查询客户列表
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @PostMapping("/customer/list")
    public Map<String,Object> getCustList(@RequestParam("page") Integer page,
                                          @RequestParam("rows") Integer rows,
                                          String customerName){
        PageInfo<Customer> custList = customerService.getCustList(page,rows,customerName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",custList.getTotal());
        map.put("rows",custList.getList());
        return map;
    }

    /**
     * 添加或者修改
     * @param customer
     * @param customerId
     * @return
     */
    @PostMapping("/customer/save")
    public ServiceVO<Customer> saveOrUpdata(Customer customer,
                 @RequestParam(value = "customerId",required = false) Integer customerId){
        if(customerId == null){
            //添加
            customerService.savaCust(customer);
        }else{
            //修改
            customerService.updataCust(customer,customerId);
        }

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("/customer/delete")
    public ServiceVO removeCusts(String ids){
        customerService.removeCusts(ids);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
}
